package offer;

import java.util.Stack;

public class offer31 {
    public  static boolean validateStackSequences(int[] pushed, int[] popped) {
       if(pushed.length==0||popped.length==0) return true;
        Stack<Integer> stack = new Stack<>();
        int startPush = 0;
        int startPop=0;
        while (startPush<pushed.length)
        {
            stack.push(pushed[startPush]);
            while (!stack.isEmpty()&&stack.peek()==popped[startPop])
            {
                stack.pop();
                startPop++;
            }
            startPush++;
        }
//        for (int i = 0; i <pushed.length ; i++) {
//            stack.push(pushed[i]);
//            while (stack.peek()==popped[start]&&!stack.isEmpty())
//            {
//                stack.pop();
//                start++;
//            }
//        }

        return stack.isEmpty();

    }

    public static void main(String[] args) {
//        [4,0,1,2,3]
//[4,2,3,0,1]
        int [] push = {1,2,3,4,5};
        int []pop = {4,5,3,2,1};
        validateStackSequences(push,pop);
    }
}
